package com.yuyou.fn.timer.task;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.DelayChangeTeacher;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.IDelayChangeTeacherService;
import com.yuyou.fn.educational.service.ITeacherService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/4/9.
 */
@JobHander(value="DealDelayTeacherMessageTask")
@Component
public class DealDelayTeacherMessageTask extends IJobHandler {

    @Resource
    private ITeacherService teacherService;

    @Resource
    private IDelayChangeTeacherService delayChangeTeacherService;


    @Override
    public ReturnT<String> execute(String... params) throws Exception {
        Date date=TimeUtils.nowDate();
        if (params!=null){
            date=Utils.parseDate(params[0],"yyyy-MM-dd");
        }
        List<DelayChangeTeacher> delayChangeTeacherList=delayChangeTeacherService.findByHappenDateStatus(date,0);

        for (DelayChangeTeacher delayChangeTeacher:delayChangeTeacherList){
            try{
                Teacher updateTeacher=new Teacher();
                updateTeacher.setTeacherId(delayChangeTeacher.getTeacherId());
                if (delayChangeTeacher.getChangeType()==1){
                    updateTeacher.setStatus(Integer.parseInt(delayChangeTeacher.getChangeData()));
                }else if (delayChangeTeacher.getChangeType()==2){
                    updateTeacher.setJobNature(Integer.parseInt(delayChangeTeacher.getChangeData()));
                }
                updateTeacher.setUpdateTime(TimeUtils.now());
                teacherService.updateSelectiveById(updateTeacher);
                delayChangeTeacherService.changeStatusById(delayChangeTeacher.getDelayChangeId(),1);

            }catch (Exception e){
                XxlJobLogger.log("延迟id为:"+delayChangeTeacher.getDelayChangeId()+"的信息处理失败");
            }

        }
        return ReturnT.SUCCESS;
    }

}
